﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FooFun.GrantFrameworks;
using System.Data.Common;
using System.Data;
using System.Collections;
using System.Configuration;

namespace AccessGrant
{
    public class DaoPermissionQuery
        : IPermissionQuery<int, int, int>
    {
        DataTable dt_set = null;

        DataTable dt_account = null;
        DataTable dt_object = null;
        DataTable dt_per = null;
        
        private DaoPermissionQuery()
        {
            dt_set 
                = DataService.FillTable("SELECT * FROM t_PermissionSets");
            dt_account = DataService.FillTable("SELECT * FROM t_Accounts");
            dt_object = DataService.FillTable("SELECT * FROM t_Forums");
            dt_per = DataService.FillTable("SELECT * FROM t_Permissions");

            InitAccountList();
            InitObjectList();
            InitPermissionList();
        }

        List<int> accountList = null;
        List<int> objectList = null;
        List<int> perList = null;

        private void InitAccountList()
        {
            accountList = new List<int>();

            foreach (DataRow row in dt_account.Rows)
            {
                accountList.Add((int)row["AccountId"]);
            }
        }

        private void InitObjectList()
        {
            objectList = new List<int>();

            foreach (DataRow row in dt_object.Rows)
            {
                objectList.Add((int)row["ForumId"]);
            }
        }

        private void InitPermissionList()
        {
            perList = new List<int>();

            foreach (DataRow row in dt_per.Rows)
            {
                perList.Add((int)row["PermissionId"]);
            }
        }

        public static DaoPermissionQuery Init()
        {
            DaoPermissionQuery dpq = new DaoPermissionQuery();

            return dpq;
        }

        #region IPermissionQuery<int,int,int> 成员

        public PermissionSet this[int obj, int user, int per]
        {
            get
            {
                foreach (DataRow row in dt_set.Rows)
                {
                    if (row["ForumId"].Equals(obj)
                        && row["AccountId"].Equals(user)
                        && row["PermissionId"].Equals(per)
                        )
                    {
                        if ((bool)row["PermissionSet"])
                        {
                            return PermissionSet.Grant;
                        }
                        else
                        {
                            return PermissionSet.Revoke;
                        }
                    }
                }

                return PermissionSet.NotSet;
            }
        }        

        public IList<int> ListObject()
        {
            return objectList;
        }        

        public IList<int> ListAccount()
        {
            return accountList;
        }

        public IList<int> ListPermission()
        {
            return perList;
        }

        public IList<Permission> ListPermissions()
        {
            List<Permission> perList = new List<Permission>();

            foreach (DataRow row in dt_per.Rows)
            {
                Permission permisson = new Permission();
                permisson.PermissionId = (int)row["PermissionId"];
                permisson.PermissionName 
                    = (string)row["Permission"];

                perList.Add(permisson);
            }

            return perList;
        }   

        #endregion

        
        
    }
}
